package com.tdam2011.grupo05.database.dao;

import java.util.Date;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;

import com.google.common.collect.Lists;
import com.tdam2011.grupo05.database.content.TdamContentProvider;
import com.tdam2011.grupo05.entities.logs.LogImp;

public class LogDao implements Dao<LogImp> {

	private static final String[] PROJECTION = { LogImp.COL_ID,
			LogImp.COL_CONTACT_NAME, LogImp.COL_DATE, LogImp.COL_DETAIL,
			LogImp.COL_TYPE, LogImp.COL_DATA };

	private final ContentResolver cr;

	public LogDao(Context context) {
		cr = context.getContentResolver();
	}

	@Override
	public long save(LogImp t) {
		final ContentValues cv = new ContentValues();
		cv.put(LogImp.COL_DATE, t.getFecha().getTime());
		cv.put(LogImp.COL_CONTACT_NAME, t.getNombreContacto());
		cv.put(LogImp.COL_DETAIL, t.getDescripcion());
		cv.put(LogImp.COL_DATA, t.getData());
		cv.put(LogImp.COL_TYPE, t.getTipo());
		Uri u = cr.insert(TdamContentProvider.LOGGER_CONTENT_URI, cv);
		return ContentUris.parseId(u);
	}

	@Override
	public void update(LogImp t) {
		final ContentValues cv = new ContentValues();
		cv.put(LogImp.COL_DATE, t.getFecha().getTime());
		cv.put(LogImp.COL_CONTACT_NAME, t.getNombreContacto());
		cv.put(LogImp.COL_DETAIL, t.getDescripcion());
		cv.put(LogImp.COL_DATA, t.getData());
		cv.put(LogImp.COL_TYPE, t.getTipo());
		cr.update(TdamContentProvider.LOGGER_CONTENT_URI, cv, BaseColumns._ID
				+ "= ?", new String[] { String.valueOf(t.getId()) });
	}

	@Override
	public void delete(LogImp t) {
		cr.delete(TdamContentProvider.LOGGER_CONTENT_URI, BaseColumns._ID
				+ "=?", new String[] { String.valueOf(t.getId()) });
	}

	@Override
	public LogImp get(long id) {
		LogImp l = null;
		Cursor c = cr.query(TdamContentProvider.LOGGER_CONTENT_URI, PROJECTION,
				BaseColumns._ID + "= ?", new String[] { String.valueOf(id) },
				"1");
		if (c.moveToFirst()) {
			l = buildLogger(c);
		}
		if (!c.isClosed()) {
			c.close();
		}
		return l;
	}

	private LogImp buildLogger(Cursor c) {
		if (c != null) {
			final LogImp l = new LogImp();
			l.setId(c.getLong(c.getColumnIndex(LogImp.COL_ID)));
			l.setDescripcion(c.getString(c.getColumnIndex(LogImp.COL_DETAIL)));
			l.setFecha(new Date(c.getLong(c.getColumnIndex(LogImp.COL_DATE))));
			l.setTipo(c.getInt(c.getColumnIndex(LogImp.COL_TYPE)));
			l.setNombreContacto(c.getString(c
					.getColumnIndex(LogImp.COL_CONTACT_NAME)));
			l.setData(c.getString(c.getColumnIndex(LogImp.COL_DATA)));
			return l;
		}
		return null;
	}

	@Override
	public List<LogImp> getAll() {
		List<LogImp> mList = Lists.newArrayList();
		return mList;
	}

	@Override
	public Cursor getCursor(String selection, String[] selectionArgs,
			String sortOrder) {
		return cr.query(TdamContentProvider.LOGGER_CONTENT_URI, PROJECTION,
				selection, selectionArgs, sortOrder);
	}

}
